<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false"%>
<html>
<head>
    <title>用户管理</title>
    <link rel="stylesheet" href="assets/ztree/zTreeStyle/zTreeStyle.css" type="text/css">

    <script type="text/javascript" src="assets/ztree/jquery.ztree.core.min.js"></script>
    <script type="text/javascript" src="assets/ztree/jquery.ztree.excheck.min.js"></script>
    <script type="text/javascript" src="assets/ztree/jquery.ztree.exedit.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="UserCtrl">
    <div class="col-md-3">
        <label>机构配置：</label>&nbsp;&nbsp
        <ui-tree url="system/user/listorg" method ="get" selected="true" key="org"></ui-tree>
    </div>
    <div class="col-md-9">
        <ui-form key="userAdd" column="2">
            <div class="row">
                <label>角色配置：</label>&nbsp;&nbsp
                <div id="role">
                </div>
                <input type="hidden" id="roleIds" value=""/>
                <input name="id" type="hidden" value="{{nodeData.id}}"/>
            </div>

            <ui-form-input name="realName" label="用户姓名:"></ui-form-input>
            <ui-form-input name="code" label="工号:"></ui-form-input>
            <ui-form-input name="mobile" label="手机号:"></ui-form-input>
            <ui-form-input name="idCard" label="身份证号:"></ui-form-input>
            <ui-form-date label="入职日期" name="hireDate"></ui-form-date>
            <ui-form-date label="离职日期" name="leaveDate"></ui-form-date>
            <ui-form-select name="onJob" label="在职状态:">
                <option value="0">否</option>
                <option value="1">是</option>
            </ui-form-select>
            <ui-form-select name="settlementType" label="结算类型">
                <option value="0">投递员价</option>
                <option value="1">合作站点价</option>
                <option value="2">合作商价</option>
                <option value="3">大型合作商价</option>
            </ui-form-select>
            <ui-form-input name="passwd" label="登录密码:"></ui-form-input>
        </ui-form>
    </div>
    <div class="row">
        <a class="btn btn-sm btn-success" onclick="back()">返回</a>
    </div>

    <script>

        angular.module('myApp', ['ui.tree','ui.form']);
        angular.module('myApp').controller('UserCtrl', function ($scope) {
            var url = "system/user/add";
            $scope.nodeData = {};
            $scope.$on('ui.form.submit',function (event,param) {
                var orgTree = $scope['org'].getSelectedNodes()||[];
                var orgCodes =[];
                for(var i =0 ;i<orgTree.length;i++){
                    if(orgTree[i].code!=null){
                        orgCodes.push(orgTree[i].code);
                    }
                }
                var roleIds = $("#roleIds").val();
                param.roleIds = roleIds;
                param.orgCodes = orgCodes.join(",");
                $.ajax({
                    url:url,
                    type:"post",
                    data:param
                }).done(function (result) {
                    if(result.code == '0000'){
                        toastr.success("新增成功");
                        if(opener && opener.reloadTable){
                            opener.reloadTable({});
                        }
                        setTimeout(function () {
                            window.location.href="system/user/index.htm";
                        },1500);
                    }else{
                        toastr.error(result.msg);
                    }
                }).fail(function (err) {

                });
            });
        });

        $(function(){
            $.ajax({
                url:"system/user/listrole",
                type:"post",
                dataType:"json",
                success:function(data){
                    if(data.code == "0000"){
                        showRole(data);
                    }else{
                        alert(data.msg);
                    }
                },
                error:function(XMLHttpRequest,text,error){
                    alert("请求出错!");
                }
            });
        });

        function showRole(data){
            var html = "";
            var roleList = data.data;
            for(var i = 0,l = roleList.length;i < l;i++){
                html += '<input name="multipleCheckbox" title="'+roleList[i].id+'" type="checkbox" onchange="change();"/>'+roleList[i].name+'&nbsp;&nbsp;';
                $("#role").html(html);
            }
        }

        function change(){
            var roleChecked = $("input[name=multipleCheckbox]:checked");
            var roleIds = "";
            roleChecked.each(function(){
                roleIds += $(this).attr("title");
                roleIds += ",";
            });
            $("#roleIds").val(roleIds);
        }

        function back(){
            window.location.href="system/user/index.htm";
        }
    </script>

</div>
</body>
</html>

